using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Order.Dto;
using JTZY.Model.Order;
using JTZY.Repository;
using JTZY.Service.Order.IOrderService;
using JTZY.Model.Goods;

namespace JTZY.Service.Order
{
    /// <summary>
    /// 订单明细表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsorderitemService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsorderitemService : BaseService<Corecmsorderitem>, ICorecmsorderitemService
    {
        /// <summary>
        /// 查询订单明细表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsorderitemDto> GetList(CorecmsorderitemQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmsorderitem, CorecmsorderitemDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsorderitem GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        public List<Corecmsorderitem> GetInfo(string orderId,long userId)
        {
            var response = Queryable()
                .LeftJoin<Corecmsuserstock>((it,us)=>it.ProductId==us.ProductId&&us.UserId== userId)
                .Where(it => it.OrderId == orderId)
                .Select((it,us)=>new Corecmsorderitem {
                    OrderId=it.OrderId,
                    Addon=!string.IsNullOrEmpty(us.SkuName)? us.SkuName:it.Addon,
                    ImageUrl = !string.IsNullOrEmpty(us.SkuImage) ? us.SkuImage : it.ImageUrl,
                    Nums = it.Nums,
                    Stock=us.Stock!=null? us.Stock.Value:0,
                    ProductId=it.ProductId
                })
                .ToList();

            return response;
        }

        /// <summary>
        /// 添加订单明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsorderitem AddCorecmsorderitem(Corecmsorderitem model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 导出订单明细表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsorderitemDto> ExportList(CorecmsorderitemQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .Select((it) => new CorecmsorderitemDto()
                {
                }, true)
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsorderitem> QueryExp(CorecmsorderitemQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsorderitem>();

            return predicate;
        }
    }
}